<template>
  <div class="flex direction-column height-100">
    <div class="button-group">
      <el-button type="primary" @click="add">新增</el-button>
    </div>
    <el-table
      v-loading="loading" class="table"
      :data="lists" height="100%"
      :header-cell-style="{
        backgroundColor: themeColor,
        color: 'white'
      }"
    >
      <div slot="empty" class="empty-lists">
        <svg-icon name="search"></svg-icon>
        <span class="empty-text">暂无数据</span>
      </div>
      <el-table-column v-for="column in columns" :key="column.prop" v-bind="column">
        <template v-if="column.prop === 'applicationIcon'" #default="{ row }">
          <svg-icon v-if="row[column.prop]" class="font-xlarge" :name="row[column.prop]"></svg-icon>
        </template>
      </el-table-column>
      <el-table-column label="操作" prop="id" width="120px" align="center">
        <template #default="{ row }">
          <td-button icon="editor" title="编辑" @click="edit(row)"></td-button>
          <td-button
            icon="ashbin" title="删除"
            icon-style="color: var(--danger-color);border-color: var(--danger-color);"
            @click="del(row)"
          ></td-button>
        </template>
      </el-table-column>
    </el-table>
    <operate-modal ref="modal" :data="modalData" :modal-title="modalTitle"
                   @save-success="onSaveSuccess"></operate-modal>
  </div>
</template>

<script>
import { queryList, del } from '@/api/sysApplication'
import TdButton from '@/components/td-button'
import OperateModal from './operate-modal'
import { confirmDel } from '@/utils'

const api = {
  queryList,
  del
}
export default {
  name: 'application-management',
  components: {
    TdButton,
    OperateModal
  },
  data () {
    return {
      loading: false,
      lists: [],
      columns: [
        {
          label: 'ID',
          prop: 'id'
        },
        {
          label: '应用名称',
          prop: 'applicationName'
        },
        {
          label: '应用图标',
          prop: 'applicationIcon'
        },
        {
          label: '应用描述',
          prop: 'remarks'
        },
      ],
      modalData: {},
      modalTitle: ''
    }
  },
  computed: {
    themeColor () {
      return this.$store.state.user.theme
    }
  },
  mounted () {
    this.getLists()
  },
  methods: {
    reload () {
      this.getLists()
    },
    getLists () {
      this.loading = true
      return api.queryList().then(res => {
        if (res.code === 200) {
          this.lists = res.data
        }
      }).finally(() => {
        this.loading = false
      })
    },
    add () {
      this.modalTitle = '新增应用'
      this.modalData = {}
      this.$nextTick(() => {
        this.$refs.modal.open()
      })
    },
    edit (row) {
      this.modalTitle = '编辑应用'
      this.modalData = {
        id: row.id,
        applicationName: row.applicationName,
        applicationIcon: row.applicationIcon,
        remarks: row.remarks
      }
      this.$nextTick(() => {
        this.$refs.modal.open()
      })
    },
    del (row) {
      confirmDel(this).then(() => {
        api.del(row.id).then((res) => {
          if (res.code === 200) {
            this.reload()
            this.$message.success('删除成功')
          }
        })
      }).catch(() => {
      })
    },
    onSaveSuccess () {
      this.reload()
    }
  }
}
</script>

<style scoped>

</style>
